package cn.gz.demo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import cn.gz.demo.R;
import cn.gz.demo.databinding.NumberBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * @author Liuyz
 * @date 2020/12/1 18:04
 */
public class NumberActivity extends BaseActivity {
    private NumberBinding binding;
    private String msg = "e1a641ad93cb59f6b4bdf80af814059be1a641ad93cb59f6b4bdf80af814059b";
    private List<String> msgList = new ArrayList<>();
    private int number1 = 0;
    private TextView tv1;
    private List<TextView> tv1List = new ArrayList<>();
    private int number2 = 0;
    private TextView tv2;
    private List<TextView> tv2List = new ArrayList<>();
    private int number3 = 0;
    private TextView tv3;
    private List<TextView> tv3List = new ArrayList<>();
    private int number4 = 0;
    private TextView tv4;
    private List<TextView> tv4List = new ArrayList<>();
    private int number5 = 0;
    private TextView tv5;
    private List<TextView> tv5List = new ArrayList<>();

    // 通过静态方法创建ScheduledExecutorService的实例
    private ScheduledExecutorService mScheduledExecutorService = Executors.newScheduledThreadPool(5);
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (number1 < msgList.size()) {
                    tv1 = new TextView(context);
                    tv1List.add(tv1);
                    tv1.setText(msgList.get(number1));
                    binding.layout1.addView(tv1);
                    if (number1 > 3) {
                        binding.layout1.removeView(tv1List.get(number1 - 4));
                    }
                    ++number1;
                }
            }else if(msg.what == 2) {
                if (number2 < msgList.size()) {
                    tv2 = new TextView(context);
                    tv2List.add(tv2);
                    tv2.setText(msgList.get(number2));
                    binding.layout2.addView(tv2);
                    if (number2 > 3) {
                        binding.layout2.removeView(tv2List.get(number2 - 4));
                    }
                    ++number2;
                }
            }else if(msg.what == 3) {
                if (number3 < msgList.size()) {
                    tv3 = new TextView(context);
                    tv3List.add(tv3);
                    tv3.setText(msgList.get(number3));
                    binding.layout3.addView(tv3);
                    if (number3 > 3) {
                        binding.layout3.removeView(tv3List.get(number3 - 4));
                    }
                    ++number3;
                }
            }else if(msg.what == 4) {
                if (number4 < msgList.size()) {
                    tv4 = new TextView(context);
                    tv4List.add(tv4);
                    tv4.setText(msgList.get(number4));
                    binding.layout4.addView(tv4);
                    if (number4 > 3) {
                        binding.layout4.removeView(tv4List.get(number4 - 4));
                    }
                    ++number4;
                }
            }else if(msg.what == 5) {
                if (number5 < msgList.size()) {
                    tv5 = new TextView(context);
                    tv5List.add(tv5);
                    tv5.setText(msgList.get(number5));
                    binding.layout5.addView(tv5);
                    if (number5 > 3) {
                        binding.layout5.removeView(tv5List.get(number5 - 4));
                    }
                    ++number5;
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_number);

        msgList.clear();
        for (int i = 0; i < msg.length(); i++) {
            msgList.add(msg.charAt(i) + "");
        }
        tv1List.clear();
        tv2List.clear();
        tv3List.clear();
        tv4List.clear();
        tv5List.clear();
        binding.layout1.removeAllViews();
        binding.layout2.removeAllViews();
        binding.layout3.removeAllViews();
        binding.layout4.removeAllViews();
        binding.layout5.removeAllViews();

        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        }, 100, 100, TimeUnit.MILLISECONDS);
        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(2);
            }
        }, 180, 180, TimeUnit.MILLISECONDS);
        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(3);
            }
        }, 120, 120, TimeUnit.MILLISECONDS);
        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(4);
            }
        }, 60, 60, TimeUnit.MILLISECONDS);
        mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(5);
            }
        }, 80, 80, TimeUnit.MILLISECONDS);
    }
}
